<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="com.google.gson.JsonArray"%>
<%@page import="com.google.gson.JsonObject"%>

<!DOCTYPE html>
<html lang="en">
<head>
	<%@ include file="/common/common-css.jsp" %>
	<style>
		.table > th {
		  vertical-align: center;
		  background-color: grey;
	}
	</style>		
</head>
<body class="no-skin">
	<div class="page-content">
		<div class="page-content-area">
			<div class="row">
				<div class="col-xs-12 col-sm-12 widget-container-col">
					<div class="widget-box widget-color-blue">
						<!-- #section:custom/widget-box.options.collapsed -->
						<div class="widget-header widget-header-small">
							<h6 class="widget-title lighter">
								<i class="ace-icon glyphicon glyphicon-th-list"></i>品牌信息
							</h6>
                            <div class="widget-toolbar no-border">
								<a href="#" data-action="collapse">
									<i class="ace-icon fa fa-chevron-up"></i>
								</a>
							</div>
						</div>
						
						<div class="widget-body">
							<div class="widget-main" >
								<form id="model" class="form-horizontal"  name="product-form" method="post" >
									<input type="hidden" name="model_id"  id="model_id" placeholder="" class="col-xs-12 col-sm-6">									
									<div id="product_comment">
											<button type="button" name="btn-add" class="btn btn-primary btn-sm no-border" onclick="addProductComment()">
														<i class="ace-icon glyphicon glyphicon-plus bigger-110"></i>添加属性</button>
														
											<table name="commentList" id="commentList" class="table table-bordered table-striped table-hover table-condensed table-responsive">
												<tr>
													<th style="width: 10%">内容</th>
													<th style="width: 10%">名称</th>
													<th style="width: 60%">值</th>
													<th style="width: 10%">备注</th>
													<th style="width: 10%">分割</th>
													<th style="width: 4%">操作</th>
												</tr>
												
	                       							<tr class="dataRow">

														<td><input type ='text' name="content" id="content"  value=" " class="commentName"  style="width:90%;"></td>
														<td><input type ='text' name="name" id="name"  value=" " class="commentName" style="width:90%;"></td>
														<td><!-- <input type ='text' name="value" id="value"  value=" " class="commentName" style="width:90%;"> -->
															<textarea rows="4" name="value" id="value" cols="100"></textarea>
														</td>
														<td><input type ='text' name="memo" id="memo"  value="无" class="commentName" style="width:90%;"></td>
														<td><input type ='text' name="sp" id="sp"  value="*" class="commentName" style="width:90%;"></td>
														<td class="ingore"><button type="button" class="btn btn-primary savepost" onclick="deleteProductComment(this)">删除</button></td><input type="hidden">
													</tr>
	                                            
											</table>
											<input type="hidden" name="commentListValue" id="commentListValue"/>	
										</div>
								</form>
							</div><!-- /.widget-main -->
						</div><!-- /.widget-body -->
					</div><!-- /.widget-box -->
				</div><!-- /.col-sm-12 -->
		</div><!-- /.row -->		
		<div class="clearfix form-actions">
			<div class="col-md-offset-3 col-md-9">
				<button class="btn  btn-inverse  no-border " onclick="doCancle()" id="delete"> 
					<i class="ace-icon fa fa-undo bigger-160"></i>
					取消
				</button>
				&nbsp; &nbsp; &nbsp;
				<button id="btn-save" name="btn-save" class="btn  btn-success  no-border" type="button" onclick="doSave()">
					<i class="ace-icon fa fa-floppy-o bigger-160"></i>
					保存
				</button>
			</div>
		</div>
	</div>
</div>
<%@ include file="/common/common-js.jsp" %>
<%@ include file="/discovery/maxlength.jsp" %>
<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<script>

	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return decodeURI(r[2]); return null;
	}
	var id=getQueryString("id");
	
	$(function(){		
		$("#model_id").val(id);
		$("#btn-save").removeAttr("disabled");
		$(".dataRow").remove();
		makeDate(id);
			
	});
    
	function makeDate(id){
			var url=ctx + "/module/getModuleList.action";
			 $.ajax({
				"url":url ,
				"type": "POST",
				"dataType": 'json',
				"data":  {"model_id":id},
				"beforeSend": function(){
					
				},
				"success": function(data){
					if(data.aaData != null){
					  	 
						var html="";
		            	$.each(data.aaData, function(index, item){
		            		html=html+"<tr class='dataRow'><td><input type ='text' readonly='readonly'  class='commentName' id='content' name='content'  style='width:90%;' value='"+item.content+"'></td>"+
		            		"<td><input type ='text' readonly='readonly'  class='commentName' id='name' name='name'  style='width:90%;' value='"+item.name+"'></td>"+
		            		"<td><textarea type ='text' readonly='readonly'  rows='4' name='value' id='value' cols='100' >"+item.value+"</textarea></td>"+
		            		"<td><input type ='text' readonly='readonly'  class='commentName' id='memo' name='memo'  style='width:90%;' value='"+item.memo+"'></td>"+
		            		"<td><input type ='text' readonly='readonly'  class='commentName' id='sp' name='sp'  style='width:90%;' value='"+item.split+"'></td>"+
		            		
		            		"<td class='ingore'><button type='button' class='btn btn-primary savepost' onclick='view("+item.id+")'>预览</button>&nbsp;&nbsp;"+
		            		"<button type='button' class='btn btn-primary savepost' onclick='editProductComment(this)'>编辑</button></td><input type='hidden'></tr>"
		            	});
		            	//"<td class='ingore'><button type='button' class='btn btn-primary savepost' onclick='deleteProductComment(this)'>删除</button>&nbsp;&nbsp;"+
		            	var row = $("#commentList tr").length;
		            	var $tr=$("#commentList tbody tr").eq(row - 1);
		            	$tr.after(html);
		            };
				},
				"error": function(){
					alert("操作失败");								 
				},
				"complete": function(){
					//解除事件
					
				}
		});  	
		
	}
	
	
	function doCancle() {
		
		try {
	      	if(parent) {
	        parent.parent['__extdialog_active'].dialog('close');
	      }
	    } catch(e) {
	      window.history.back();
	    }
	} 

	function deleteSource(row) {
		
		var index = $(row).parent().parent().find('.rowIndex').text();
		var $tr = $("#sourceList tr").eq(index);
		$tr.remove();
		
		$("#sourceList tbody tr .rowIndex").each(function(trindex,tritem){
			$(tritem).text(trindex + 1);
		});
	}


function doSave() {
	
	var seachboxs=$("input[name='content']");
	var len=seachboxs.length;
	
	var content ="";
	
	$("input[name='content']").each(function(i,obj){
		content+=$(obj).val();
		if(i!=(len-1)){
			content+="@#";
		}
	});
	
	var name="";
	$("input[name='name']").each(function(i,obj){
		name+=$(obj).val();
		if(i!=(len-1)){
			name+="@#";
		}
	});
	
	var value="";
	$("textarea[name='value']").each(function(i,obj){
		value+=$(obj).val();
		if(i!=(len-1)){
			value+="@#";
		}
	});

	var memo="";
	$("input[name='memo']").each(function(i,obj){
		memo+=$(obj).val();
		if(i!=(len-1)){
			memo+="@#";
		}
	});
	
	var sp="";
	$("input[name='sp']").each(function(i,obj){
		sp+=$(obj).val();
		if(i!=(len-1)){
			sp+="@#";
		}
	});
	
 	    var url=ctx + "/module/saveModule.action";
		 $.ajax({
			"url":url ,
			"type": "POST",
			"data":  {"content":content,"name":name,"value":value,"memo":memo,"sp":sp,"model_id":id},
			"beforeSend": function(){
				$("#btn-save").attr("disabled","disabled");
			},
			"success": function(data){
				var req=JSON.parse(data);
				var status=req.status;
				var message=req.message;
				
				if(status=="1"){
					alert(message);
				}else{
					alert("操作成功");
				}
				parent.parent['__extdialog_active'].dialog('close');
				parent.main_frame.opsuccessFunc(getQueryString("id"));	
			},
			"error": function(){
				alert("操作失败");								 
			},
			"complete": function(){
				//解除事件
				
				$("#btn-save").attr("disabled","");
			}
	});     

}

function addProductComment() {
	
	var row = $("#commentList tr").length;
    var html = '<tr class="dataRow"><td><input type ="text"  class="commentName" id="content" name="content"  style="width:90%;" value=" "></td>' +
		'<td><input type ="text"  class="commentName" id="name" name="name"  style="width:90%;" value=" "></td>'+
		'<td><textarea rows="4" name="value" id="value" cols="100"></textarea></td>'+
		'<td><input type ="text"  class="commentName" id="memo" name="memo"  style="width:90%;" value="无"></td>'+
		'<td><input type ="text"  class="commentName" id="sp" name="sp"  style="width:90%;" value="*"></td><input type="hidden"></tr>';//+
		//'<td class="ingore"><button type="button" class="btn btn-primary savepost" onclick="deleteProductComment(this)">删除</button></td><input type="hidden"></tr>';
   
		var $tr=$("#commentList tbody tr").eq(row - 1);
    $tr.after(html);
    
    commentOperation();
    
}

function deleteProductComment(row) {
	
	var $tr = $(row).parent().parent();
	$tr.remove();
}

function editProductComment(row){
	var tr = $(row).parent().parent();
	$(tr).find("input").each(function(){
		$(this).removeAttr("readonly");
	});
	$(tr).find("textarea").removeAttr("readonly");
}

function view(id){
	 var url = "/module/view.action?id="+id;
	url = encodeURI(url); 
	openDialogWithRand("/module/moduleView.jsp?id="+id,1000,1000,"预览信息",function (data){});
}
</script>	
</body>
</html>
